﻿$(document).ready(function() {
    if (!Object.keys) Object.keys = function(o) {
        if (o !== Object(o))
            throw new TypeError('Object.keys called on non-object');
        var ret = [], p;
        for (p in o) if (Object.prototype.hasOwnProperty.call(o, p)) ret.push(p);
        return ret;
    }
    var pollFinished = false;
    var allParticipants = new Array();
    $('#publicMessage').click(function() {
        $(this).val("");
        $(this).unbind();
    });
    $('#publicMessageButton').click(function() {
        if ($('#publicMessage').val() == "" || $('#publicMessage').val() == "Enter a message to send to all web users...") {
            $('#publicMessage').val("");
            $('#errorMessage').text("Message cannot be blank.");
            $('#publicMessage').focus(function() {
                $('#errorMessage').text("");
                $(this).unbind();
            });
        }
        else {
            $.ajax({
                type: 'POST',
                url: "/Participant/SendPublicMessage",
                data: { messageText: $('#publicMessage').val(), pollID: $('#pollID').val() },
                success: function() {
                    $('#publicMessage').val("");
                    $('#publicMessageButton').attr("disabled", true);
                    $('#publicMessageButton').val("Sent");
                    $('#publicMessage').click(function() {
                        $('#publicMessageButton').val("Send");
                        $('#publicMessageButton').attr("disabled", false);
                    });
                }
            });
        }
    });
    var username;
    $('.privateMessageButton').each(function() {
        $(this).click(function() {
            username = $(this).next().text();
            if ($(this).parent().parent().children().first().next().text() == "") {
                $('#privateMessageFirstName').text("");
                $('#privateMessageLastName').text("");
                $('#privateMessageDeviceID').text($(this).parent().parent().children().first().text());
            }
            else {
                $('#privateMessageDeviceID').text("");
                $('#privateMessageFirstName').text($(this).parent().parent().children().first().next().text());
                $('#privateMessageLastName').text($(this).parent().parent().children().first().next().next().text());
            }
            $('#sendPrivateMessage').unbind();
            $('#sendPrivateMessage').click(function() {
                if ($('#privateMessage').val() != "") {
                    $.ajax({
                        type: 'POST',
                        url: "/Participant/SendPrivateMessage",
                        data: { messageText: $('#privateMessage').val(), username: username, pollID: $('#pollID').val() }
                    });
                }
                $('#privateMessageDialog').addClass("none");
            });
            var offset = $(this).offset();
            $('#privateMessageDialog').css("top", offset.top - 85);
            $('#privateMessageDialog').css("left", offset.left - 320);
            $('#privateMessage').val("");
            $('#privateMessageDialog').removeClass("none");
        });
    });
    $('#cancelPrivateMessage').click(function() {
        $('#privateMessageDialog').addClass("none");
    });
    $('#controlArrowContainer').click(function() {
        participants = new Array();
        if (actionList.length > 0) {
            if (questionID > 0) {
                var a = questionID;
                $.ajax({
                    type: 'POST',
                    url: "/Question/GetRespondedDeviceIDs",
                    data: { questionID: questionID },
                    success: function(response) {
                        for (var i = 0; i < response.length; i++) {
                            $('#' + response[i] + a).css("background-color", "Green");
                            if (!(response[i] in allParticipants)) {
                                allParticipants[response[i]] = true;
                                $.ajax({
                                    type: 'POST',
                                    url: "/Participant/MarkAsAttended",
                                    data: { deviceID: response[i], pollID: $('#pollID').val() }
                                });
                            }
                        }
                    }
                });
            }
            if (questionID > 0 && (questionType == "singleResponse" || questionType == "multiResponse")) {
                $.ajax({
                    type: 'POST',
                    url: "/Respond/TrimExcessResponses",
                    data: { questionID: questionID },
                    aysnc: false
                });
            }
            var nextURL = actionList.pop();
            $.ajax({
                type: 'POST',
                url: "/Poll/SetPageInstruction",
                data: { pollID: $('#pollID').val(), nextWebURL: nextURL[0], nextScreenURL: nextURL[1] }
            });
            questionID = nextURL[2];
            questionType = nextURL[3];
            if (questionType == "numericResponse") {
                responseHandler = "shortResponseHandler";
            }
            else {
                responseHandler = "optionResponseHandler";
            }
            $('#pollQuestionsContainer').children().first().remove();
            $('#pollQuestionsContainer').children().first().addClass("greenBorder");
        }
        else {
            if (!pollFinished) {
                $('#pollQuestionsContainer').children().first().remove();
                $('#pollQuestionsContainer').children().first().addClass("greenBorder");
                pollFinished = true;
                $.ajax({
                    type: 'POST',
                    url: "/Poll/SetPollFinished",
                    data: { pollID: $('#pollID').val() }
                });
            }
        }
    });
    var firstURL = actionList.pop();
    $.ajax({
        type: 'POST',
        url: "/Poll/SetPageInstruction",
        data: { pollID: $('#pollID').val(), nextWebURL: firstURL[0], nextScreenURL: firstURL[1] }
    });
    window.setInterval(function() {
        $.ajax({
            type: 'POST',
            url: "/Poll/GetLatestQuestionResults",
            data: { pollID: $('#pollID').val() },
            success: function(response) {
                for (var i = 0; i < response.length; i++) {
                    externalData.setValue(i, 0, "Q" + (i + 1));
                    externalData.setValue(i, 1, response[i][0]);
                    externalData.setValue(i, 2, response[i][1]);
                }
                externalChart.draw(externalData, { backgroundColor: '#EAF0F4', colors: ['blue', 'green'], width: 460, height: 100, legend: 'none', vAxis: { textPosition: 'none' }, chartArea: { top: 0, left: 0, width: "100%"} });
            }
        });
    }, 5000);
    $('#controlArrow').mouseover(function() {
        $(this).attr("src", "../../Resources/rightarrow2.png");
    });
    $('#controlArrow').mouseout(function() {
        $(this).attr("src", "../../Resources/rightarrow.png");
    });
    $('#receiverErrorOK').click(function() {
        window.location.reload(true);
    });
    $('#receiverErrorCancel').click(function() {
        $('#receiverErrorDialogContainer').addClass("none");
    });
    $.ajax({
        type: 'POST',
        url: "/Poll/CopyParticipantsToLinkedPoll",
        data: { pollID: $('#pollID').val() },
        async: false
    });
    if (!testPoll) {
        $.ajax({
            type: 'POST',
            url: "/Poll/SetStartDate",
            data: { pollID: $('#pollID').val() }
        });
        $.ajax({
            type: 'POST',
            url: "/Poll/RemoveTestParticipants",
            data: { pollID: $('#pollID').val() }
        });
    }
});